﻿#include "WinFunc.h"
#include "Misc.h"

namespace Base {

	// 
	std::string WinFunc::GetWorkingDir() {
		static char currentDir[MAX_PATH];
		if (GetCurrentDirectoryA(MAX_PATH, currentDir) != 0) {
			return currentDir;//std::string(currentDir);
		}
		else {
			Debug::ThrowOrMessageBox("获取workingDir 失败!");
		}
	}

	std::wstring WinFunc::StrToWstr(const std::string& str) {
		int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
		wchar_t* wideStr = new wchar_t[len];
		MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wideStr, len);
		std::wstring wstr(wideStr);
		delete[] wideStr;
		return wstr;
	}

	std::string WinFunc::WstrToStr(const std::wstring& wstr) {
		int bufSize = WideCharToMultiByte(CP_UTF8, NULL, wstr.c_str()
			, -1, NULL, 0, NULL, FALSE);
		char *sp = new char[bufSize];
		WideCharToMultiByte(CP_ACP, NULL, wstr.c_str(), -1, sp, bufSize, NULL, FALSE);
		std::string str(sp);
		delete[] sp;
		return str;
	}

	void WinFunc::CreateConsole() {
		AllocConsole();
		freopen("conout$", "w", stdout);
		freopen("conout$", "w", stderr);
	}
}